package org.lanqiao.oa.controller;

import org.lanqiao.oa.mapper.ResultInfo;
import org.lanqiao.oa.mapper.User;
import org.lanqiao.oa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/sys")
public class SystemController {

    @Autowired
    public UserService userService;

    /**
     * 用户登录
     * @param request
     * @param user
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResultInfo login(HttpServletRequest request, User user){
        String ip = request.getRemoteAddr();
        try{
            User login = userService.login(user, ip);
            request.getSession().setAttribute("loginUser",login);
        }catch (Exception e){
            return ResultInfo.getResultInfo(false,e.getMessage());
        }
        return  ResultInfo.getResultInfo(true);
    }

    /**
     * 登录成功跳转页面
     * @param request
     * @return
     */
    @RequestMapping("/toMain")
    public String toMain(HttpServletRequest request){
        Object loginUser = request.getSession().getAttribute("loginUser");
        if(loginUser==null){
            return "";
        }else {
            return "forward:/WEB-INF/main.jsp";
        }
    }

}
